<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <span>
        <button @click="reduceNum">-</button>
        <div>{{num}}</div>
        <button @click="addNum">+</button>
      </span>
      <div>单价：{{price}}</div>
      <div>总价：{{num*price}}</div>
    </div>
  </body>
  <script>
    var vm = new Vue({
      el: "#app",
      data: {
        // 商品数量
        num: 0,
        // 单价
        price: 10,
      },
      methods: {
        // 减少商品数量
        reduceNum() {
          if (this.num > 0) {
            this.num--;
          }
        },
        // 增加商品数量
        addNum() {
          this.num++;
        },
      },
    });
  </script>
</html>
